<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * class ajaxAddNode
    ****
    * This class will create a new node via an AJAX method
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    ***/
    class ajaxKarmaPage extends ajaxAction{

        public function process(){
            
            /*
                @param id
                @param user
            */
            
            //see if I have already rated this page
            if(!$this->db->getOne("SELECT ip FROM page_karma_history_anon WHERE pageid = ".(int)$_REQUEST['id']." AND ip = ".$this->db->qstr($_SESSION['user']))){
                
                if($_REQUEST['type'] == 'give'){
                    $_REQUEST['karma'] = 1;
                }else{
                    $_REQUEST['karma'] = -1;
                }
                
                $this->db->Execute("INSERT INTO page_karma_history_anon(pageid, ip, karma) VALUES(".(int)$_REQUEST['id'].",".$this->db->qstr($_SESSION['user']).",".($_REQUEST['karma']>0?1:-1).")");
                                      
                //recalculate karma for the page
                if(!$this->db->getOne("SELECT karma FROM page_karma WHERE pageid = ".(int)$_REQUEST['id'])){
                    $this->db->Execute("INSERT INTO page_karma(pageid, votes, karma) VALUES(".(int)$_REQUEST['id'].",1,".(int)$_REQUEST['karma'].")");
                }else{
                    $this->db->Execute("UPDATE page_karma SET votes = votes + 1, karma = karma + ".(int)$_REQUEST['karma']." WHERE pageid = ".(int)$_REQUEST['id']);
                }                                
            }
            
            $this->r->karma = $this->db->getOne("SELECT karma FROM page_karma WHERE pageid = ".(int)$_REQUEST['id']);                        
        }
    }
?>